//
// Created by liushuai on 18-8-23.
//
#include "Node.hpp"
#include "Tree.hpp"
Tree::Tree() {
    m_pRoot = new Node();
}
Node* Tree::searchNode(int nodeindex) {
    return m_pRoot->searchNode(nodeindex);
}
Tree::~Tree() {
    m_pRoot->DeleteNode();
}
bool Tree::AddNode(int nodeindex, int direction, Node *pNode) {
    Node *temp = searchNode(nodeindex);
    if(temp == nullptr)
        return false;
    Node *node = new Node();
    if(node == nullptr)
        return false;

    node->index = pNode->index;
    node->data = pNode->data;
    node->pParent = temp;
    if(direction == 1)
        temp->pLChild = node;
    if(direction == 2)
        temp->pRChild = node;
    return false;

}
bool Tree::DeleteNode(int nodeindex, Node *pNode) {
    Node *temp = searchNode(nodeindex);
    if(temp == nullptr)
        return false;
    if(pNode != nullptr)
        pNode->data = temp->data;
    //删除找到的节点
    temp->DeleteNode();
    return true;
}
void Tree::PreorderTraversal()  {
    m_pRoot->PerorderTraversal();
}
void Tree::InorderTraversal() {
    m_pRoot->InorderTraversal();
}
void Tree::PosorderTraval() {
    m_pRoot->PosorderTraversal();
}